/**
 *  Copyright (C) 2011  Thomas ROBIN
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package triseries;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;

/**
 * @author tshepo
 *
 */
public class UnZip {

	private static final boolean DEBUG = (Main.debug || MyProperties.debug.equalsIgnoreCase("UnZip"));
	private ZipFile zipfile;

	public UnZip(File zippedFile) {
		if(DEBUG) System.err.println("UnZip.UnZip("+zippedFile.getName()+")");
		try {
			if (Main.debug || MyProperties.verbose) {
				System.out.println("Ouverture de " + zippedFile.getName());
			}
			zipfile = new ZipFile(zippedFile);
		} catch (ZipException e) {
			Main.processExeption("in UnZip.UnZip :\n"+e.getStackTrace().toString());
		} catch (IOException e) {
			Main.processExeption("in UnZip.UnZip :\n"+e.getStackTrace().toString());
		}
	}

	public int Extract(String file, String name) {
		if(DEBUG) System.err.println("UnZip.Extract("+file+","+name+")");

		
			ZipEntry entry = zipfile.getEntry(file);

			if (entry == null) {
				if (zipfile.size() == 1) {
					entry = zipfile.entries().nextElement();
				} else {
					Enumeration<? extends ZipEntry> entry_enum = zipfile
							.entries();
					while (entry_enum.hasMoreElements()) {
						entry = entry_enum.nextElement();
						if (entry.getName().contains(file))
							break;
						else
							entry = null;
					}
					if (entry == null) {
						System.err.println("le soutitre " + file
								+ " n'est pas dans l'archive");
						return -1;
					}
				}
			}
			if (Main.debug || MyProperties.verbose)
				System.out.println("Extraction de " + entry.getName());
			int BUFFER = 2048;
			FileOutputStream fos = null;
			try {
				fos = new FileOutputStream(name);
			} catch (FileNotFoundException e) {
				Main.processExeption("UnZip.Extract :\n"+e.getStackTrace().toString());
				return -2;
			}
			BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);

			InputStream zin = null;
			try {
				zin = zipfile.getInputStream(entry);
			} catch (IOException e) {
				Main.processExeption("UnZip.Extract :\n"+e.getStackTrace().toString());
				return -3;
			}

			int count;
			byte data[] = new byte[BUFFER];

			try {
				while ((count = zin.read(data, 0, BUFFER)) != -1) {
					dest.write(data, 0, count);
				}
			} catch (IOException e) {
				Main.processExeption("UnZip.Extract :\n"+e.getStackTrace().toString());
				return -4;
			}

			try {
				dest.flush();
			} catch (IOException e) {
				Main.processExeption("UnZip.Extract :\n"+e.getStackTrace().toString());
				return -5;
			}
			try {
				dest.close();
			} catch (IOException e) {
				Main.processExeption("UnZip.Extract :\n"+e.getStackTrace().toString());
				return -6;
			}
			try {
				zin.close();
			} catch (IOException e) {
				Main.processExeption("UnZip.Extract :\n"+e.getStackTrace().toString());
				return -7;
			}
			return 0;
	}
}
